package com.example.demo.tool;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class ResponseResult<T> {
    private String msg;
    private T data;
    private String code;
    @JSONField(
            serialzeFeatures = {SerializerFeature.NotWriteDefaultValue}
    )
    private long count;

    public ResponseResult() {
    }

    public static <T> ResponseResult<T> check(boolean flag){
        if (flag) {
            return success();
        }
        return error();
    }

    public static <T> ResponseResult<T> success() {
        ResponseResult result = new ResponseResult();
        result.setCode(ResponseEnum.SUCCESS.getCode());
        result.setMsg(ResponseEnum.SUCCESS.getMessage());
        return result;
    }

    public static <T> ResponseResult<T> success(T data) {
        ResponseResult result = new ResponseResult();
        result.setCode(ResponseEnum.SUCCESS.getCode());
        result.setMsg(ResponseEnum.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    public static <T> ResponseResult<T> success(T data, long count) {
        ResponseResult result = new ResponseResult();
        result.setCode(ResponseEnum.SUCCESS.getCode());
        result.setMsg(ResponseEnum.SUCCESS.getMessage());
        result.setData(data);
        result.setCount(count);
        return result;
    }

    public static <T> ResponseResult<T> error() {
        ResponseResult result = new ResponseResult();
        result.setCode(ResponseEnum.FAILURE.getCode());
        result.setMsg(ResponseEnum.FAILURE.getMessage());
        return result;
    }

    public static <T> ResponseResult<T> error(T data) {
        ResponseResult result = new ResponseResult();
        result.setCode(ResponseEnum.FAILURE.getCode());
        result.setMsg(ResponseEnum.FAILURE.getMessage());
        result.setData(data);
        return result;
    }

    public static <T> ResponseResult<T> error(ResponseEnum responseEnum, T data) {
        ResponseResult result = new ResponseResult();
        result.setCode(responseEnum.getCode());
        result.setMsg(responseEnum.getMessage());
        result.setData(data);
        return result;
    }

    public static ResponseResult error(ResponseEnum responseEnum, String msg) {
        ResponseResult result = new ResponseResult();
        result.setCode(responseEnum.getCode());
        result.setMsg(msg);
        return result;
    }

    public static ResponseResult error(ResponseEnum responseEnum) {
        ResponseResult result = new ResponseResult();
        result.setCode(responseEnum.getCode());
        result.setMsg(responseEnum.getMessage());
        return result;
    }

    public static <T> ResponseResult<T> breaker() {
        ResponseResult result = new ResponseResult();
        result.setCode(ResponseEnum.BREAKER.getCode());
        result.setMsg(ResponseEnum.BREAKER.getMessage());
        return result;
    }

    public static <T> ResponseResult<T> breaker(T data) {
        ResponseResult result = new ResponseResult();
        result.setCode(ResponseEnum.BREAKER.getCode());
        result.setMsg(ResponseEnum.BREAKER.getMessage());
        result.setData(data);
        return result;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public long getCount() {
        return this.count;
    }

    public void setCount(long count) {
        this.count = count;
    }
}
